import {login,getUserInfo,getMenu} from '@/api/login.js'

const user = {
    //state专门用来保存共享的状态值
    state:{
        //登录状态
        login:false,
        token: localStorage.getItem("token"),
        roles: localStorage.getItem("roles"),
        userInfo: JSON.parse(localStorage.getItem("userInfo")),
        menuList: JSON.parse(localStorage.getItem("menuList")),

    },
    //mutations 专门写方法，用来更新status中的值
    mutations:{
        SET_MENU:(state,menuList)=>{
            state.menuList = menuList;
            localStorage.setItem("menuList",JSON.stringify(menuList));
        },
        doLogin(state){
            state.login=true;
        },
        doLogout(state){
            state.login=false;
        },
        setToken:(state,token)=>{
            console.log(token);
            state.token = token;
            localStorage.setItem("token",token);
        },
        setUserInfo:(state,userInfo)=>{
            state.userInfo = userInfo;
            localStorage.setItem("userInfo",JSON.stringify(userInfo));
        },
        setRoles:(state,roles)=>{
            state.roles = roles;
            localStorage.setItem("roles",roles);
            
        },
        removeUser:state=>{
            state.token = '';
            state.roles = [];
            state.userInfo ={};
            localStorage.removeItem("token");
            localStorage.removeItem("roles")
            localStorage.removeItem("userInfo");
            localStorage.removeItem("menuList")
        },
        // USER_SIGNOUT(state){
        //     Object.key(state).foreach((foo)=>{
        //     Vue.delete(state,foo);
        //     })
        // }
    },
    actions:{
        //登录
        Login({commit},params){
            return new Promise((resolve,reject)=>{
                login(params).then(res=>{
                    commit('setToken',res.data);
                    resolve(res)
                }).catch(err=>{
                    reject(err);
                })
            })
        },
        //获取用户信息
        GetUserInfo({commit},state){
            return new Promise((resolve,reject)=>{
                getUserInfo().then(res=>{
                    commit('setRoles',res.data.roles);
                    commit('setUserInfo',res.data.userInfo);
                    resolve(res);
                }).catch(err=>{
                    reject(err);
                })
            })
        },   
        //获取菜单
        GetMenu({commit}){
            return new Promise((resolve,reject)=>{
                getMenu().then(res=>{
                    console.log(res);
                    commit('SET_MENU',res.data.menuList)
                    resolve(res);
                }).catch(err=>{
                    reject(err);
                })
            })
        },
        // 登出
        LogOut({commit}) {
          return new Promise((resolve, reject) => {
            commit('removeUser');
                resolve()
                reject(error)
            })
        },

    },
}
export default user;